package com.yloveg.impl;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yloveg.common.DateUtil;
import com.yloveg.dao.SRoleMapper;
import com.yloveg.dao.VActivityMapper;
import com.yloveg.dao.VGiftMapper;
import com.yloveg.dao.VPlayerMapper;
import com.yloveg.entity.SRole;
import com.yloveg.entity.VActivity;
import com.yloveg.entity.VGift;
import com.yloveg.entity.VPlayer;
import com.yloveg.service.SRoleService;
import com.yloveg.service.VActivityService;
import com.yloveg.service.VGiftService;
import com.yloveg.service.VPlayerService;

@Service
public class VPlayerServiceImpl implements VPlayerService {

	@Autowired
    private VPlayerMapper vPlayerMapper;
	
	@Autowired
    private VActivityMapper vActivityMapper;
	

	@Override
	public PageInfo<VPlayer> queryPlayerPage(int page, int limit) {
		PageHelper.startPage(page, limit);
		List<VPlayer> vPlayerList = vPlayerMapper.queryVPlayer();
		PageInfo<VPlayer> resultPage=new PageInfo<VPlayer>(vPlayerList);
		return resultPage;
	}
	
	@Override
	public PageInfo<VPlayer> queryActivityPageByactivity(int page, int limit, int activityid) {
		PageHelper.startPage(page, limit);
		List<VPlayer> vPlayerList = vPlayerMapper.queryVPlayerByactivity(activityid);
		PageInfo<VPlayer> resultPage=new PageInfo<VPlayer>(vPlayerList);
		return resultPage;
	}

	@Override
	public int insertPlayer(VPlayer vplayer) {
		vplayer.setCreateTime(new Date());
		vplayer.setSts("Y");
		return vPlayerMapper.insert(vplayer);
	}

	@Override
	public int updateByPrimaryKeySelective(VPlayer vplayer) {
		vplayer.setUpdateTime(new Date());
		return vPlayerMapper.updateByPrimaryKeySelective(vplayer);
	}

	@Override
	public VPlayer selectByPrimaryKey(Integer id) {
		return vPlayerMapper.selectByPrimaryKey(id);
	}

	@Override
	public int delPlayer(Integer id) {
		return vPlayerMapper.deleteByPrimaryKey(id);
	}

	@Override
	public List<Map<String, Object>> queryPlayerMap(VGift vgift) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<VPlayer> queryPlayer() {
		List<VPlayer> vPlayerList = vPlayerMapper.queryVPlayer();
		return vPlayerList;
	}

	@Override
	@Cacheable(value="queryActivityPageByactivityIsY",key = "#page+'-'+#limit+'-'+#activityid+'-'+#title")
	public PageInfo<VPlayer> queryActivityPageByactivityIsY(int page, int limit,int activityid,String title) {
		Map map = new HashMap();
		map.put("activityid", activityid);
		map.put("title", title);
		//map.put("sts", "Y");
		PageHelper.startPage(page, limit);
		List<VPlayer> vPlayerList = vPlayerMapper.queryVPlayerByactivityAndTitle(map);
		PageInfo<VPlayer> resultPage=new PageInfo<VPlayer>(vPlayerList);
		return resultPage;
	}
	
	@Override
	@Cacheable(value="queryActivityPageByactivityIsY",key = "#page+'-'+#limit+'-'+#activityid+'-'+#title")
	public PageInfo<VPlayer> queryActivityPageByactivityIsYN(int page, int limit,int activityid,String title) {
		Map map = new HashMap();
		map.put("activityid", activityid);
		map.put("title", title);
		//map.put("sts", "Y");
		PageHelper.startPage(page, limit);
		List<VPlayer> vPlayerList = vPlayerMapper.queryVPlayerByactivityAndTitleN(map);
		PageInfo<VPlayer> resultPage=new PageInfo<VPlayer>(vPlayerList);
		return resultPage;
	}

	@Override
	public List<VPlayer> queryPlayerByActDesc(int activityid) {
		List<VPlayer> vPlayerList = vPlayerMapper.queryVPlayerByactivity(activityid);
		return vPlayerList;
	}

	@Override
	public List<VPlayer> queryPlayerByActAndYDesc(int activityid) {
		List<VPlayer> vPlayerList = vPlayerMapper.queryVPlayerByactivityAndY(activityid);
		return vPlayerList;
	}

	@Override
	public List<VPlayer> queryVPlayerByRecord(VPlayer vplayer) {
		List<VPlayer> vPlayerList = vPlayerMapper.queryVPlayerByRecord(vplayer);
		return vPlayerList;
	}

	@Override
	public List<VPlayer> queryPlayerAll(int activityid) {
		List<VPlayer> vPlayerList = vPlayerMapper.queryVPlayerAll(activityid);
		return vPlayerList;
	}
	
	@Override
	public List<VPlayer> queryPlayerAll2(int activityid) {
		List<VPlayer> vPlayerList = vPlayerMapper.queryVPlayerAll2(activityid);
		return vPlayerList;
	}

	@Override
	@Cacheable(value="returnWeixinDate",key="#activityid")
	public JSONObject returnWeixinDate(int activityid) {
	   	SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
    	JSONObject json = new JSONObject(); 
    	//活动信息
    	VActivity activity=vActivityMapper.selectByPrimaryKey(activityid);
    	json.put("title", activity.getTitle());
    	json.put("start_time", activity.getStartTime());
    	json.put("end_time", activity.getEndTime());
    	json.put("backmusic", activity.getBackmusic());
    	json.put("adpic", activity.getAdpic());
    	json.put("adpicurl", activity.getAdpicurl());
    	json.put("prizepic", activity.getPrizepic());
    	json.put("headpic", activity.getHeadpic());
    	json.put("activitypic", activity.getActivitpic());
    	json.put("flag", activity.getFlag());
    	//统计信息
    	//List<VPlayer> vlist= this.queryPlayerByActDesc(activityid);
    	//List<VVisitLog> vvisitlog= vVisitorLogService.selectByActivityid(activityid);
    	//点赞次数需汇总选手得票数，因为存在后台管理人员手动增加票数情况
    	/*int thumbs=0;
    	for(VPlayer vp : vlist) {
    		thumbs=thumbs+vp.getPoll();
    	}*/
    	//json.put("enroll", vlist.size());
    	//json.put("thumbs", thumbs);
    	json.put("enroll", activity.getPalyernum());
    	json.put("thumbs", activity.getThumbnum());
    	json.put("visits", activity.getTotalnum());
    	try {
    		Date end= sdf.parse(activity.getEndTime());
    		String days=DateUtil.timesBetween(end, new Date());
    		if(end.before(new Date())) {
    			json.put("overDays", "活动结束");
    		}else {
    			json.put("overDays", "距离活动结束还有"+days);
    		}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return json;
	}

	@Override
	@Cacheable(value="getRankList",key="#activityid")
	public JSONObject getRankList(int activityid) {
    	JSONObject json = new JSONObject(); 
    	VActivity activity=vActivityMapper.selectByPrimaryKey(activityid);
    	List<VPlayer> vlist= this.queryPlayerByActAndYDesc(activityid);
    	//List<VVisitLog> vvisitlog= vVisitorLogService.selectByActivityid(activityid);
    	json.put("rank", vlist);
    	/*List<VPlayer> vplay= this.queryPlayerByActDesc(activityid);
    	int thumbs=0;
    	for(VPlayer vp : vplay) {
    		thumbs=thumbs+vp.getPoll();
    	}
    	json.put("enroll", vlist.size());
    	json.put("thumbs", thumbs);*/
    	json.put("enroll", activity.getPalyernum());
    	json.put("thumbs", activity.getThumbnum());
    	json.put("visits", activity.getTotalnum());
    	json.put("headpic", activity.getHeadpic());
    	return json;
	}

	@Override
	@Cacheable(value="getPlayerDetail",key="#playerid")
	public JSONObject getPlayerDetail(int playerid) {
    	VPlayer vp = this.selectByPrimaryKey(playerid);
    	int id= vp.getId();
    	int activityid= vp.getActivityid();
    	String url ="http://tp3.ergreggvffv.top/vote-admin/free/toPlayerDetail?playerid="+id+"&activityid="+activityid;
    	int num=0;
    	VActivity activity=vActivityMapper.selectByPrimaryKey(activityid);
    	List<VPlayer> vlist= this.queryPlayerByActDesc(activityid);
    	for(VPlayer v : vlist) {
    		num=num+1;
    		if(v.getId()==id) {
    			break;
    		}
    	}
    	JSONObject jsonObj = (JSONObject) JSON.toJSON(vp);
    	String giftid=activity.getGift();
    	if(giftid==null || "".equals(giftid)) {
    		jsonObj.put("flag", "N");
    	}else {
    		jsonObj.put("flag", "Y");
    	}
    	jsonObj.put("order", num);
    	jsonObj.put("title", activity.getTitle());
    	jsonObj.put("activitpic", activity.getActivitpic());
    	jsonObj.put("endtime", activity.getEndTime());
    	jsonObj.put("url", url);
    	return jsonObj;
	}

	@Override
	public int queryVPlayerNO(VPlayer record) {
		// TODO Auto-generated method stub
		return vPlayerMapper.queryVPlayerNO(record);
	}

}
